<?php
require_once 'header.php';

// 检查是否已登录
if (!isLoggedIn()) {
    $_SESSION['flash_message'] = '请先登录再查看购物车';
    $_SESSION['flash_message_type'] = 'warning';
    header('Location: login.php');
    exit;
}

// 获取用户购物车
$user_id = $_SESSION['user_id'];
$cart_items = getUserCart($user_id);
$cart_total = getCartTotal($cart_items);
?>

<div class="container">
    <h1 class="mb-4">购物车</h1>
    
    <?php if (count($cart_items) > 0): ?>
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">购物车商品 (<?php echo count($cart_items); ?>)</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>商品</th>
                                <th>单价</th>
                                <th>数量</th>
                                <th>小计</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($cart_items as $item): ?>
                                <tr>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <img src="<?php echo !empty($item['image']) ? $item['image'] : 'https://via.placeholder.com/50x50?text=电影票'; ?>" 
                                                class="img-thumbnail mr-3" style="width: 50px; height: 50px;" 
                                                alt="<?php echo $item['name']; ?>">
                                            <div>
                                                <a href="product.php?id=<?php echo $item['product_id']; ?>" class="text-dark">
                                                    <?php echo $item['name']; ?>
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                    <td>¥<?php echo number_format($item['price'], 2); ?></td>
                                    <td>
                                        <form action="update_cart.php" method="post" class="d-flex align-items-center">
                                            <input type="hidden" name="cart_id" value="<?php echo $item['id']; ?>">
                                            <button type="submit" name="action" value="decrease" class="btn btn-sm btn-outline-secondary">-</button>
                                            <input type="text" class="form-control form-control-sm mx-2 text-center" style="width: 50px;" value="<?php echo $item['quantity']; ?>" readonly>
                                            <button type="submit" name="action" value="increase" class="btn btn-sm btn-outline-secondary">+</button>
                                        </form>
                                    </td>
                                    <td>¥<?php echo number_format($item['price'] * $item['quantity'], 2); ?></td>
                                    <td>
                                        <form action="update_cart.php" method="post">
                                            <input type="hidden" name="cart_id" value="<?php echo $item['id']; ?>">
                                            <button type="submit" name="action" value="remove" class="btn btn-sm btn-danger">
                                                <i class="fas fa-trash-alt"></i> 删除
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">优惠券</h5>
                    </div>
                    <div class="card-body">
                        <form action="cart.php" method="post" class="form-inline">
                            <div class="form-group mr-2">
                                <input type="text" class="form-control" name="coupon_code" placeholder="输入优惠码">
                            </div>
                            <button type="submit" class="btn btn-primary">应用</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">订单汇总</h5>
                    </div>
                    <div class="card-body">
                        <div class="d-flex justify-content-between mb-2">
                            <span>商品总计:</span>
                            <span>¥<?php echo number_format($cart_total, 2); ?></span>
                        </div>
                        <div class="d-flex justify-content-between mb-2">
                            <span>优惠:</span>
                            <span class="text-danger">-¥0.00</span>
                        </div>
                        <hr>
                        <div class="d-flex justify-content-between mb-3">
                            <span class="font-weight-bold">应付总额:</span>
                            <span class="font-weight-bold text-danger">¥<?php echo number_format($cart_total, 2); ?></span>
                        </div>
                        <form action="checkout.php" method="post">
                            <button type="submit" class="btn btn-success btn-block">
                                <i class="fas fa-lock mr-2"></i>结算
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="mt-4">
            <a href="products.php" class="btn btn-outline-primary">
                <i class="fas fa-arrow-left mr-2"></i>继续购物
            </a>
            <button onclick="clearCart()" class="btn btn-outline-danger ml-2">
                <i class="fas fa-trash-alt mr-2"></i>清空购物车
            </button>
        </div>
        
    <?php else: ?>
        <div class="alert alert-info">
            <h4 class="alert-heading">购物车为空!</h4>
            <p>您的购物车中还没有商品，赶快去选购吧！</p>
            <hr>
            <p class="mb-0">
                <a href="products.php" class="btn btn-primary">
                    <i class="fas fa-shopping-bag mr-2"></i>浏览商品
                </a>
            </p>
        </div>
    <?php endif; ?>
</div>

<script>
function clearCart() {
    if (confirm('确定要清空购物车吗？')) {
        window.location.href = 'update_cart.php?action=clear';
    }
}
</script>

<?php require_once 'footer.php'; ?> 